package com.xc.ezplan.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.ezplan.admin.entity.AdminTencentConfig;
import com.xc.ezplan.admin.entity.AdminTencentConfig;
import com.xc.ezplan.admin.mapper.AdminTencentConfigMapper;
import com.xc.ezplan.admin.service.AdminTencentConfigService;
import com.xc.ezplan.admin.mapper.AdminTencentConfigMapper;
import com.xc.ezplan.admin.vo.AdminTencentConfigVo;
import com.xc.ezplan.cache.constants.RedisKey;
import com.xc.ezplan.cache.utils.CacheUtil;
import com.xc.ezplan.core.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lixingchen
 * @date 2022/12/07
 * @description 腾讯云配置实现类
 */
@Service
@Primary
@RequiredArgsConstructor
@CacheConfig(cacheNames = "tencentConfig")
public class AdminTencentConfigServiceImpl extends ServiceImpl<AdminTencentConfigMapper, AdminTencentConfig>
    implements AdminTencentConfigService{
    private final AdminTencentConfigMapper adminTencentConfigMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createConfig(AdminTencentConfigVo adminTencentConfigVo) {
        AdminTencentConfig adminTencentConfig = new AdminTencentConfig();
        BeanUtils.copyProperties(adminTencentConfigVo,adminTencentConfig);
        adminTencentConfigMapper.insert(adminTencentConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteConfig(List<Long> ids) {
        adminTencentConfigMapper.deleteBatchIds(ids);
        CacheUtil.deleteCache(RedisKey.TENCENT_CONFIG_ID,ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateConfig(AdminTencentConfigVo adminTencentConfigVo) {
        AdminTencentConfig adminTencentConfig = new AdminTencentConfig();
        BeanUtils.copyProperties(adminTencentConfigVo,adminTencentConfig);
        adminTencentConfigMapper.update(adminTencentConfig,new UpdateWrapper<AdminTencentConfig>().lambda().
                eq(AdminTencentConfig::getId,adminTencentConfig.getId()));
        CacheUtil.deleteCache(RedisKey.TENCENT_CONFIG_ID,adminTencentConfigVo.getId());
    }

    @Override
    public Object queryAllConfig(String keyword) {
        LambdaQueryWrapper<AdminTencentConfig> queryWrapper = new QueryWrapper<AdminTencentConfig>().lambda().like(
                StringUtils.isNotBlank(keyword), AdminTencentConfig::getBucket,
                keyword);
        return adminTencentConfigMapper.selectList(queryWrapper);
    }

    @Override
    @Cacheable(key = "'id:' + #p0")
    public Object queryConfig(Long id) {
        return adminTencentConfigMapper.selectById(id);
    }
}




